package com.quhaodian.user.oauth.impl;

import com.quhaodian.user.oauth.api.OauthHandler;
import com.quhaodian.user.oauth.domain.GitHubUser;
import com.quhaodian.user.oauth.domain.OauthResponse;
import com.quhaodian.user.oauth.domain.TokenResponse;
import com.quhaodian.utils.http.Connection;
import com.quhaodian.utils.http.HttpConnection;
import com.google.gson.Gson;

public class GitHubHander implements OauthHandler {
    @Override
    public void setKey(String key) {

    }

    @Override
    public void setSecret(String secret) {

    }

    @Override
    public OauthResponse login(String access_token, String openid) {
        GitHubUser result = null;

        Gson gson = new Gson();

        Connection info = HttpConnection.connect("https://api.github.com/user");

        info.data("access_token", access_token);
        info.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
        String infojson;
        try {
            infojson = info.execute().body();
            result = gson.fromJson(infojson, GitHubUser.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public TokenResponse getToken(String code) {
        return null;
    }
}
